﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using Microsoft.Win32;
using Maticsoft.Common;

namespace WEB.contract
{
    public partial class OtherView : System.Web.UI.Page
    {
        
        DataSet ds = new DataSet();
        ContractManage Bll = new ContractManage();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
           
                if (!IsPostBack)
                {
                    databinds();
                }//数据载入


        }

        protected void databinds()
        {
            if (Request.QueryString["CID"] != null)
            {
                string id = Request.QueryString["CID"].ToString();
                ds = Bll.GetViewList(" CID= " + id);
            }
            gvOtherView.DataSource = ds;
            gvOtherView.DataBind();
        }

        protected void gvOtherView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Dow")
            {
                string index = e.CommandArgument.ToString();
                int id = (Int32)gvOtherView.DataKeys[Convert.ToInt32(index)].Value;
                string fileName = Bll.GetModel(id).Images.ToString();
                if (!System.IO.File.Exists(fileName))
                {
                    MessageBox.Show(this, "文件丢失！");
                }
                else
                {
                    //执行下载
                    if (System.IO.Path.GetExtension(fileName) == ".html")
                    {
                        fileName = fileName.Replace(".html", ".doc");
                    }
                    DownloadFile(fileName);
                }
            }
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        public void DownloadFile(string filename)
        {
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);

            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;

            string encodefileName = System.Web.HttpUtility.UrlEncode(saveFileName, System.Text.Encoding.UTF8);
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);

            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();

            Response.End();
        }

        protected void gvOtherView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOtherView.PageIndex = e.NewPageIndex;
            gvOtherView.DataBind();//分页
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            #region 删除

            int id = Convert.ToInt32(((Button)(sender)).CommandArgument.ToString());//获取ID

            if (Bll.Delete(id))//删除
            {
                
                MessageBox.Show(this, "删除成功！");
               
            }
            else
            {
                MessageBox.Show(this, "删除失败！");
            }
            databinds();
        
         
            #endregion

        }

        protected void return_Click(object sender, EventArgs e)
        {
            Response.Redirect("./ContractView.aspx");
        }
    }


}